/*1942 game clone
 * Authors:
 * Abdollah Tabareh
 * Bharat Bhushan Konka
 * Martin Svensson 
 * Richard Zianne
 * Tigran Harutyunyan
 * 
 * 2010
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Game1942.Handelers
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Wave1Handeler : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Texture2D _texture;
        private Game _game;
        private int _counter = 0;
        private Vector2 _waveDirection;
        private int _speed = 1;


        private GameScene _gameScene;

        // List of active planes
        protected List<Enemy1> _planes;
        public Wave1Handeler(Game game, ref Texture2D texture)
            : base(game)
        {
            _texture = texture;
            _planes = new List<Enemy1>();
            _game = game;
            _waveDirection = Direction.Down;
        }

        internal void Initialize(GameScene gameScene)
        {
            _gameScene = gameScene;
            Initialize();
        }
        
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            //_planes.Clear;
            for (int i = 0; i < 10; i++)
            {
                AddPlane(i);
            }

            base.Initialize();
        }

        private void AddPlane(int planeNumber)
        {
            Enemy1 newEnemy = new Enemy1(_game, ref _texture);
            newEnemy._index = planeNumber;
            newEnemy.putInPosition(calculatePosition(planeNumber, newEnemy));
            
            

            //newEnemy.putInPosition((float)80 * planeNumber, 0);
            newEnemy._direction = Direction.Down;
            newEnemy._planeSpeed = _speed;
            _planes.Add(newEnemy);


        }

        private Vector2 calculatePosition(int planeNumber, Enemy1 enemy)
        {

            Vector2 value = new Vector2();

            if (planeNumber % 2 == 0)//place to right
            {
                value.X = (Util.ScreenWidth / 2) + (enemy._planeWith * planeNumber);
                value.Y = 0;
            }
            else //plaseLeft
            {
                value.X = (Util.ScreenWidth / 2) - (enemy._planeWith * planeNumber) - enemy._planeWith;
                value.Y = 0;
            }
            return value;
        }

        



        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            
            
            // Update Planes
            for (int i = 0; i < _planes.Count; i++)
            {
                if (_planes[i]._isDead)
                {
                    _planes.RemoveAt(i);
                    
                }
            }
            loop();
            //loopLeft();

           
            
                  
                   
            for (int i = 0; i < _planes.Count; i++)
            {
                _planes[i]._index = i;
                if (_planes[i].getPosition().Y == Util.ScreenHeight)
                {
                    _planes[i].putInPosition(calculatePosition(_planes[i]._index,_planes[i]));
                }
                //Fire a shot
                if ((!(_planes[i]._direction == Direction.Left)
                    || !(_planes[i]._direction == Direction.Right)
                    )&& (Util.Instance.RandomGenerator.Next(500) == 1))
                {
                   _gameScene.fireBullit(_planes[i]._direction,_planes[i]._position,false);

                }

                
                _planes[i]._direction = _waveDirection;
                _planes[i]._planeSpeed = _speed;
                _planes[i].Update(gameTime);
                
                
            }
            
            base.Update(gameTime);
        }



        private void loop()
        {
            if (_counter < 30)
            {
                _waveDirection = Direction.Down;
                _speed = 2;
                _counter++;

            }

            else if (_counter < 60)
            {
                _waveDirection = Direction.DownRight;
                _speed = 1;
                _counter++;

            }
            else if (_counter < 90)
            {
                _waveDirection = Direction.Right;
                _counter++;

            }
            else if (_counter < 120)
            {
                _waveDirection = Direction.UpRight;
                _counter++;

            }
            else if (_counter < 150)
            {
                _waveDirection = Direction.Up;
                _counter++;

            }
            else if (_counter < 180)
            {
                _waveDirection = Direction.UpLeft;
                _counter++;

            }
            else if (_counter < 210)
            {
                _waveDirection = Direction.Left;
                _counter++;

            }
            else if (_counter < 240)
            {
                _waveDirection = Direction.DownLeft;
                _counter++;

            }
            else if (_counter < 270)
            {
                _waveDirection = Direction.Down;
                _speed = 2;
                _counter++;

            }
            else if (_counter < 300)
            {
                _waveDirection = Direction.Down;
                _speed = 2;
                _counter ++;

            }
            else if (_counter < 330)
            {
                _waveDirection = Direction.DownLeft;
                _speed = 1;
                _counter++;

            }
            else if (_counter < 360)
            {
                _waveDirection = Direction.Left;

                _counter++;

            }
            else if (_counter < 390)
            {
                _waveDirection = Direction.UpLeft;

                _counter++;

            }
            else if (_counter < 420)
            {
                _waveDirection = Direction.Up;

                _counter++;

            }
            else if (_counter < 450)
            {
                _waveDirection = Direction.UpRight;

                _counter++;

            }
            else if (_counter < 480)
            {
                _waveDirection = Direction.Right;

                _counter++;

            }
            else if (_counter < 510)
            {
                _waveDirection = Direction.DownRight;

                _counter++;

            }
            else if (_counter < 540)
            {
                _waveDirection = Direction.Down;
                _speed = 2;
                _counter =0;

            }
        }

        /// <summary>
        /// Allows the game component draw your content in game screen
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            
            for (int i = 0; i < _planes.Count; i++)
            {
                _planes[i].Draw(gameTime);
            }

            base.Draw(gameTime);
        }
        public void CheckCollision()
        {

        }

        public Enemy1[] getEnemys()
        {
            return _planes.ToArray();
        }
        
    }
}